package com.feisi.controller;

import com.feisi.domain.Customer;
import com.feisi.domain.User;
import com.feisi.service.CustomerService;
import com.feisi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserService userService;

    /**
     * 查询所有客户
     *
     * @param request
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(HttpServletRequest request) {
        System.out.println("表现层:查询所有客户");
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        List<Customer> customers = customerService.findAll((page - 1) * limit, limit);
        System.out.println(customers);
        int count = customerService.customerTotal();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", customers);
        return map;
    }

    /**
     * 根据客户名称查客户
     */
    @RequestMapping("/findByName")
    public @ResponseBody
    Map<String, Object> findByName(@RequestParam("cu_name") String cu_name, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        System.out.println("根据客户名称查客户");
        System.out.println("cu_name="+cu_name+",page="+page+",limit="+limit);
        List<Customer> customers = customerService.findByName(cu_name, (page - 1) * limit, limit);
        int count = customerService.customerTotalByName(cu_name);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", customers);
        System.out.println(map);
        return map;
    }



    /**
     * 根据根据用户id查出此用户负责的客户
     * 实现 我负责的客户 功能
     * @param cu_userid
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findByUserid")
    public @ResponseBody
    Map<String, Object> findByUserid(String cu_userid, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        System.out.println("根据当前用户ID名称查询客户");
        System.out.println("cu_userid="+cu_userid+",page="+page+",limit="+limit);
        Integer cu_UserId = Integer.valueOf(cu_userid);
        List<Customer> customers = customerService.findByUserid(cu_UserId, (page - 1) * limit, limit);
        int count = customerService.customerTotalByUserid(cu_UserId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", customers);
        return map;

    }


    /**
     * 新增客户
     *
     * @param cu_name
     * @param cu_source
     * @param cu_phone
     * @param cu_e_mail
     * @param cu_industry
     * @param cu_level
     * @param cu_address
     * @param cu_next_time
     * @param cu_principal
     * @param cu_comment
     //* @param username
     * @throws ParseException
     */
    @RequestMapping("/saveCustomer")
    public @ResponseBody
    Map<String, Object> saveCustomer(String cu_name, String cu_source, String cu_phone, String cu_e_mail,
                         String cu_industry, String cu_level, String cu_address, String cu_next_time,
                         String cu_comment,String cu_creator,Integer cu_principal) throws ParseException {
        System.out.println("cu_creator="+cu_creator);
        System.out.println(cu_next_time);
        System.out.println(cu_principal);
        Customer customer = new Customer();
        customer.setCu_name(cu_name);
        customer.setCu_source(cu_source);
        customer.setCu_phone(cu_phone);
        customer.setCu_e_mail(cu_e_mail);
        customer.setCu_industry(cu_industry);
        customer.setCu_level(cu_level);
        customer.setCu_address(cu_address);
        customer.setCu_creator(cu_creator);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        customer.setCu_create_time(df.format(new Date()));

        System.out.println("cu_next_time="+cu_next_time);
        customer.setCu_next_time(cu_next_time);

        customer.setCu_comment(cu_comment);
        customer.setCu_creator(cu_creator);
        //User user = userService.findByName(username, 0, 10).get(0);
        User user = userService.findUserById(cu_principal);
        customer.setUser(user);
        System.out.println(customer);
        customerService.saveCustomer(customer);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }

    /**
     * 根据id删除客户
     * 批量删除
     *
     * @param ids
     *
     */
    @RequestMapping(value = "/deleteCustomer",method = {RequestMethod.POST})
    public @ResponseBody
    Map<String, Object> deleteCustomer(@RequestBody String[] ids) {
        Integer[] id = new Integer[ids.length];
        System.out.println(ids.length);
        for (int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
            Integer idOne = Integer.valueOf(ids[i]);
            id[i]=idOne;

        }
        customerService.deleteCustomer(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }




    /**
     * 修改客户
     *@param  id
     * @param cu_name
     * @param cu_source
     * @param cu_phone
     * @param cu_e_mail
     * @param cu_industry
     * @param cu_level
     * @param cu_address
     * @param cu_next_time
     * @param cu_principal
     * @param cu_comment
    //* @param username
     * @throws ParseException
     */
    @RequestMapping("/updateCustomer")
    public @ResponseBody
    String updateCustomer(String id, String cu_name, String cu_source, String cu_phone, String cu_e_mail,
                                     String cu_industry, String cu_level, String cu_address,String cu_next_time,
                                     String cu_comment,Integer cu_principal) throws ParseException {
        System.out.println("id="+id+",cu_name="+cu_name+",cu_source="+cu_source+
                ",cu_phone="+cu_phone+",cu_e_mail="+cu_e_mail+",cu_industry="+cu_industry+
                ",cu_level="+cu_level+",cu_address="+cu_address+",cu_next_time="+cu_next_time+
                ",cu_comment="+cu_comment+",cu_principal="+cu_principal);
        Customer customer = new Customer();
        customer.setId(Integer.valueOf(id));
        customer.setCu_name(cu_name);
        customer.setCu_source(cu_source);
        customer.setCu_phone(cu_phone);
        customer.setCu_e_mail(cu_e_mail);
        customer.setCu_industry(cu_industry);
        customer.setCu_level(cu_level);
        customer.setCu_address(cu_address);
        customer.setCu_next_time(cu_next_time);
        customer.setCu_comment(cu_comment);

        //User user = userService.findByName(username, 0, 10).get(0);
        User user = userService.findUserById(cu_principal);
        customer.setUser(user);
        System.out.println(customer);

        customerService.updateCustomer(customer);
        /*Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);*/
        return "success";
    }


}
